package com.bit.frige;


import java.util.Calendar;
import java.util.StringTokenizer;

import com.bit.frige.DB.FoodDTO;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ImageKikanView extends LinearLayout {

	   private ImageView mIcon;
	   private TextView mText01;
	   private TextView mText02;   
	   
	   public ImageKikanView(Context context, FoodDTO fooddto) {
	      super(context);
	      int Dday;
	      // Layout Inflation
	      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	      inflater.inflate(R.layout.btn_kikan, this, true);

	      // Set Icon
	      mIcon = (ImageView) findViewById(R.id.imageView1);
	      setIcon(fooddto.getCateno(),fooddto.getSelfdate());
	      
	      
	      // Set Text 01
	      mText01 = (TextView) findViewById(R.id.text1);
	      setText(fooddto.getFoodname(),fooddto.getSelfdate());   
	      
	      
	   }
	   
	   public void setText(String foodname, String selfdate) {
	      int Dday;
	      Dday=dDaySet(selfdate);
	      if(Dday>=0){   
	      mText01.setText("D-"+Dday + "\n" + foodname);   
	      }else if( Dday<0){   
	      Dday= Dday*(-1);   
	      mText01.setText("D+"+Dday + "\n" + foodname);   
	      }
	      }
	   
	//이미지 아이콘과 음식큰분류 매치
	   public void setIcon(int Cateno, String selfdate) {
	      int Dday;
	      Dday=dDaySet(selfdate);
	         if(Cateno==0&&Dday>=0){
	         mIcon.setImageResource(R.drawable.food1);
	      }else if(Cateno==0&&Dday<0){
	         mIcon.setImageResource(R.drawable.greyfood1);
	      }else if(Cateno==1&&Dday>=0){
	         mIcon.setImageResource(R.drawable.food2);
	      }else if(Cateno==1&&Dday<0){
	         mIcon.setImageResource(R.drawable.greyfood2);
	      } else if(Cateno==2&&Dday>=0){
	         mIcon.setImageResource(R.drawable.food3);
	      } else if(Cateno==2&&Dday<0){
	         mIcon.setImageResource(R.drawable.greyfood3);
	      }else if(Cateno==3&&Dday>=0){
	         mIcon.setImageResource(R.drawable.food4);
	      }else if(Cateno==3&&Dday<0){
	         mIcon.setImageResource(R.drawable.greyfood4);
	      }else if(Cateno==4&&Dday>=0){
	         mIcon.setImageResource(R.drawable.food5);
	      }else if(Cateno==4&&Dday<0){
	         mIcon.setImageResource(R.drawable.greyfood5);
	      }else if(Cateno==5&&Dday>=0){
	         mIcon.setImageResource(R.drawable.food6);
	      }else if(Cateno==5&&Dday<0){
	         mIcon.setImageResource(R.drawable.greyfood6);
	      }else if(Cateno==6&&Dday>=0){
	         mIcon.setImageResource(R.drawable.food7);
	      }else if(Cateno==6&&Dday<0){
	         mIcon.setImageResource(R.drawable.greyfood7);
	      }else if(Cateno==7&&Dday>=0){
		         mIcon.setImageResource(R.drawable.food8);
	      }else if(Cateno==7&&Dday<0){
		         mIcon.setImageResource(R.drawable.greyfood9);
	      }
	   }
	   
	   

	   public int dDaySet(String selfdate){
	      String date = selfdate.trim();
	      Log.d("ImageKikanView.selfdate","날짜  "+date);
	      StringTokenizer stz = new StringTokenizer(date,"-");// 문자열을 개별적인 토큰으로 분리하는 방법

	      int []day = new int[3];

	      for( int k=0; k<3; k++){
	         day[k]= Integer.parseInt(stz.nextToken());
	      }
	      Calendar date1 = Calendar.getInstance();// 현재날짜
	      Calendar date2 = Calendar.getInstance();// 유통기한 날짜
	      int year = day[0];
	      int month= day[1];
	      int d = day[2];
	      date2.set(year,month-1,d); 
	      long difference = (date2.getTimeInMillis()-date1.getTimeInMillis())/1000;
	      int dday =(int) (difference/(24*60*60));
	      
	      return dday;

	   }


	}